<?php
    use User\Model\OptionModel;
?>
<!-- page heading start-->
<div class="page-heading">
    <h3>
        意见箱-员工
    </h3>

</div>
<!-- page heading end-->
<div class="row">
    <div class="col-sm-12">
        <section class="panel">
            <header class="panel-heading">
                <a class="btn btn-default" href="/User/Tool/option">意见箱</a>
                <a class="btn btn-default" href="/User/Tool/option_dept">部门</a>
                <a class="btn btn-default" href="/User/Tool/option_person">员工</a>
            </header>
            <div class="panel-body">
                <div>
                    <a class="btn btn-success" onclick="$('#myModal').modal('show');">增加</a>
                </div>
                <form class="form-inline" action="/User/Tool/option_person">
                    名称：<input type="text" class="form-control" name="name" value="<?php echo I('get.name', '', '');?>">
                    电话号码：<input type="text" class="form-control" name="telephone" value="<?php echo I('get.telephone', '', '');?>">
                    <button type="submit" class="btn">搜索</button>
                </form>
                <table class="table table-hover general-table table-bordered">
                    <thead>
                        <tr>
                            <th>姓名</th>
                            <th>手机号码</th>
                            <th>所属部门</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach($person_arr as $val){?>
                    <tr>
                        <td>
                            <span>
                                <?php echo $val['name'];?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo $val['telephone'];?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo $dept_arr[$val['dept_id']];?>
                            </span>
                        </td>
                        <td>
                            <a class="btn btn-default" href="#"
                                onclick="<?php echo 'return edit(' . $val['person_id'] . ', this)';?>" data-telephone="<?php echo $val['telephone']?>"
                            data-name="<?php echo $val['name']?>" data-dept="<?php echo $val['dept_id']?>" data-level="<?php echo $val['level']?>"
                            >编辑</a>
                            <a class="btn btn-default" href="#"
                               onclick="<?php echo 'return delete_data(' . $val['person_id'] .')';?>">删除</a>
                        </td>
                    </tr>
                    <?php }?>
                    </tbody>
                </table>
                <div class="pages">
                    {$page}
                </div>
            </div>
        </section>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <input id="person_id" value="" type="hidden">
            <div class="modal-body">
                名称：<input type="text" class="form-control" id="name">
                手机号码：<input type="text" class="form-control" id="telephone">
                所属部门：<select id="dept_id" class="form-control">
                        <?php
                        foreach($dept_arr as $key=>$dept){
                            echo "<option value=" . $key . ">" . $dept . "</option>";
                        }
                        ?>
                    </select>
                <input type="radio" name="level" value="0">员工
                <input type="radio"  name="level" value="1">领导
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="update()">提交更改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<script>
    function delete_data(id){
        $.ajax({
            data:{person_id:id},
            dataType:'json',
            url:'/User/Tool/option_person_del',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
    function update() {
        var id= $("#person_id").val();
        var name=$("#name").val();
        var telephone=$("#telephone").val();
        var dept_id=$("#dept_id").find("option:selected").val();
        var level=$("input[name='level']:checked").val();
        $.ajax({
            data:{
                person_id:id,
                telephone:telephone,
                name:name,
                dept_id:dept_id,
                level:level
            },
            dataType:'json',
            url:'/User/Tool/option_person_edit',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
    function edit(id, obj){
        var s_name = $(obj).attr("data-name");
        var s_telephone = $(obj).attr("data-telephone");
        var s_dept_id = $(obj).attr("data-dept");
        var s_level = $(obj).attr("data-level");
        $("#person_id").val(id);
        $("#name").val(s_name);
        $("#telephone").val(s_telephone);
        $("#dept_id").val(s_dept_id);
        $("input[name='level']").eq(s_level).prop("checked","checked");
        $('#myModal').modal('show');
    }
    $('#myModal').on('hide.bs.modal', function () {
        $("#id").val("");
        $("#name").val("");
        $("#telephone").val("");
        $("#dept_id").val("");
        $("input[name='level']").eq(1).removeAttr("checked");
    })
</script>